package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cmccgz.baselibrary.base.BaseHolder;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.AppConfig;
import com.cmccgz.udacs_landlord_webapp.bean.UnlockRecord;
import com.cmccgz.udacs_landlord_webapp.bean.WebConfig;
import com.cmccgz.udacs_landlord_webapp.main.unlock.RecordDetailActivity;
import com.cmccgz.udacs_landlord_webapp.main.web.NWebActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: June
 * @Descripion:
 * @date 2017/6/19 10:29
 */

public class UnlockRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final LayoutInflater inflater;
    private Context context;
    private List<UnlockRecord> data;

    public UnlockRecordAdapter(Context context) {
        this.context = context;
        this.data = new ArrayList<>();
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case -1:
                return new EmptyViewHolder(inflater.inflate(R.layout.item_empty, parent, false));
            case 1:
                return new NormalViewHolder(inflater.inflate(R.layout.item_unlock_record, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case -1:
                ((EmptyViewHolder) holder).tv_empty.setText("暂无进出记录！");
                break;
            case 1:
                ((NormalViewHolder) holder).instanceView(position);
                break;
        }
    }


    @Override
    public int getItemCount() {
        return data == null||data.size()==0 ? 1 : data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return data==null||data.size()==0?-1:1;
    }

    private class NormalViewHolder extends BaseHolder {

        private ImageView iv_icon;
        private TextView tv_user;
        private TextView tv_date;
        private TextView tv_building;

        public NormalViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void assignViews() {
            iv_icon = (ImageView) findViewById(R.id.iv_icon);
            tv_user = (TextView) findViewById(R.id.tv_user);
            tv_date = (TextView) findViewById(R.id.tv_date);
            tv_building = (TextView) findViewById(R.id.tv_building);
        }

        @Override
        public void instanceView(int position) {
            final UnlockRecord unlockRecord = data.get(position);
            tv_user.setText(TextUtils.isEmpty(unlockRecord.getUser()) ||
                    "null".equalsIgnoreCase(unlockRecord.getUser()) ? "" : unlockRecord.getUser());
            tv_date.setText(unlockRecord.getDate());
            tv_building.setText(unlockRecord.getBuilding());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = itemView.getContext();
                    /* WebConfig webConfig = WebConfig.get().setTitle("").setUrl("http://192.168.3.77:8080/WEBAPP/test.html");
                    Intent intent = new Intent(context, NWebActivity.class);
                    intent.putExtra(NWebActivity.KEY_CONFIG,webConfig);
                    context.startActivity(intent);*/
                   Intent intent = new Intent(context, RecordDetailActivity.class);
                    intent.putExtra(RecordDetailActivity.KEY_REC_ID,unlockRecord.getRec_id());
                    context.startActivity(intent);
                }
            });
            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Context context = itemView.getContext();
                    String rec_id = unlockRecord.getRec_id();
                    String inOutType = unlockRecord.getInOutType();
                    WebConfig webConfig = WebConfig.get().setTitle("").setUrl(AppConfig.getBaseIp()+"/h5/inAndOutDetail.html?id="+rec_id+"&inOutType="+inOutType);
                    Intent intent = new Intent(context, NWebActivity.class);
                    intent.putExtra(NWebActivity.KEY_CONFIG,webConfig);
                    context.startActivity(intent);
                    return true;
                }
            });
        }
    }
    private class EmptyViewHolder extends RecyclerView.ViewHolder {
        private final TextView tv_empty;

        public EmptyViewHolder(View itemView) {
            super(itemView);
            tv_empty = ((TextView) itemView.findViewById(R.id.tv_empty));
        }
    }
    public void changeData(List<UnlockRecord> list) {
        this.data.clear();
        this.data.addAll(list);
        notifyDataSetChanged();
    }

    public void appendData(List<UnlockRecord> list) {
        if (list != null && list.size() > 0) {
            this.data.addAll(list);
        }
        notifyDataSetChanged();
    }
}
